package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.AdminBean;
import com.woniuxy.educationplatform.bean.GoodsBean;
import com.woniuxy.educationplatform.service.ISystemService;
import com.woniuxy.educationplatform.util.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author unreleased
 * @date 2020/12/8 14:54
 */
@RestController
@RequestMapping("/system")
public class SystemController {
    @Resource
    private ISystemService systemServiceImpl;
    /**
     *
     * @param loginName 登录名
     * @param password 密码
     * @param remember 是否记住我
     * @return
     */
    @RequestMapping("/login")
    public ResultUtil login(String loginName,String password,boolean remember){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.SUCCESS).setMessage("登录成功");
        //获得登录主体对象
        Subject subject = SecurityUtils.getSubject();
        //测试代码  解决bug
        subject.logout();
        //判断当前主体，有没有被认证过
        if(!subject.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(loginName,password);
            try{
                if(remember){
                    token.setRememberMe(true);
                }
                //主体登录（触发Realm中 doGetAuthenticationInfo()）
                subject.login(token);
            }catch (UnknownAccountException e ){
                System.out.println("账号不存在");
                resultUtil.setCode(ResultUtil.ERROR);
                resultUtil.setMessage("账号不存在");
            }catch (IncorrectCredentialsException e){
                System.out.println("密码错误");
                resultUtil.setCode(ResultUtil.ERROR);
                resultUtil.setMessage("密码错误");
            }catch (Exception e){
                e.printStackTrace();
                resultUtil.setCode(ResultUtil.ERROR);
                resultUtil.setMessage("系统繁忙");
            }
        }else {
            //根据主体对象 获得会话
            Session session = subject.getSession();
            //从会话中取得登录信息
            AdminBean adminBean = (AdminBean)session.getAttribute("user");
            //将密码的明文转换为密文再对比
            String pwd = new SimpleHash("MD5",password,loginName,1024).toString();
            if(!adminBean.getPassword().equals(pwd)){
                resultUtil.setCode(ResultUtil.ERROR);
                resultUtil.setMessage("登录名或密码不正确");
                //退出系统
                subject.logout();
            }
        }
        return resultUtil;
    }
    @RequestMapping("/quit")
    public ResultUtil logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ResultUtil().setCode(ResultUtil.SUCCESS);
    }
    @RequestMapping("/sold")
    public ResultUtil findGoodsSoldTop5(){
        List<GoodsBean> goodsSoldTop5 = systemServiceImpl.findGoodsSoldTop5();
        ResultUtil<List<GoodsBean>> resultUtil = new ResultUtil<>();
        if(goodsSoldTop5.size()==0){
            return resultUtil.setCode(ResultUtil.ERROR).setMessage("好像出错了哟");
        }
        return resultUtil.setCode(ResultUtil.SUCCESS).setData(goodsSoldTop5);
    }
    @RequestMapping("/visited")
    public ResultUtil findGoodsVisitedTop5(){
        List<GoodsBean> visitedTop5 = systemServiceImpl.findGoodsVisitedTop5();
        ResultUtil<List<GoodsBean>> resultUtil = new ResultUtil<>();
        if(visitedTop5.size()==0){
            return resultUtil.setCode(ResultUtil.ERROR).setMessage("好像出错了哟");
        }
        return resultUtil.setCode(ResultUtil.SUCCESS).setData(visitedTop5);
    }

}
